package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Setmeal;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface SetmealDao {
    //按条件分页查询
    @Select("<script> select * from t_setmeal<if test=\"queryString!=null and queryString!=''\">where code=#{queryString} or name=#{queryString} or helpCode=#{queryString}</if></script>")
    Page<Setmeal> findByCondition(@Param("queryString") String queryString);

    //添加套餐信息
    @Insert("insert into t_setmeal(id,name,code,helpCode,sex,age,price,remark,attention,img) values(#{id},#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "id", keyColumn = "id", resultType = Integer.class)
    void add(Setmeal setmeal);

    // //添加套餐和检查组信息到中间表
    @Insert("insert into t_setmeal_checkgroup(setmeal_id,checkgroup_id) values(#{id},#{checkgroupId})")
    void setSetmealAndCheckGroup(@Param("id") Integer id, @Param("checkgroupId") Integer checkgroupId);

    //根据id查询 套餐信息
    @Select("select * from t_setmeal where id=#{id}")
    Setmeal findById(Integer id);

    //根据套餐id查询 检查组
    @Select("select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id}")
    List<Integer> findCheckGroupBySetmealId(Integer id);

    //修改套餐
    @Update("update t_setmeal set name=#{name},code=#{code},helpCode=#{helpCode},sex=#{sex},age=#{age},price=#{price},remark=#{remark},attention=#{attention},img=#{img} where id=#{id}")
    void edit(Setmeal setmeal);

    //删除套餐对应所有检查组
    @Delete("delete from t_setmeal_checkgroup where setmeal_id=#{setmeal_id}")
    void deleteAssocication(Integer setmeal_id);

    //查找所有套餐信息
    @Select("select * from t_setmeal")
    List<Setmeal> findAll();

    //删除 一条套餐信息
    @Delete("delete from t_setmeal where id=#{id}")
    void delete(Integer id);
    //==========================================================
    //根据套餐id查询检查组
    @Select("select * from t_setmeal where id=#{id}")
    @Results({
            @Result(property = "id", column = "id",id = true),
            @Result(property = "checkGroups",column = "id",many = @Many(select = "com.itheima.dao.CheckGroupDao.findCheckGroupBySetmealId"))
    })
    Setmeal findDetailById(Integer id);

    @Select("SELECT s.name name, COUNT(o.id) value FROM t_order o,t_setmeal s WHERE o.setmeal_id=s.id GROUP BY s.name")
    List<Map<String, Object>> findBySetmealCount();
}
